package com.ntschy.taizhou.controller;

import com.ntschy.taizhou.dao.HelloListDao;
import com.ntschy.taizhou.entity.TestUser;
import com.ntschy.taizhou.service1.InventoryAfterApproveRule;
import com.ntschy.taizhou.service1.UserTansService;
import com.ntschy.taizhou.service1.UserTansService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/test")
/* 类注解 */
@Transactional
public class HelloTestController {

    @Autowired
    HelloListDao helloListDao;

    @Autowired
    UserTansService userTansService;

    @Autowired
    UserTansService2 userTansService2;


    @Autowired
    InventoryAfterApproveRule  inventoryAfterApproveRule;



    @PutMapping("/insert")
    /* TransactionSynchronization  事务管理器的用法和理解  */
    public Object insertUser( /* 参数注解 */  @RequestBody TestUser testUser) {
        //System.out.println(testUser.toString());
        userTansService.insertUser1(testUser);

        testUser.setName(testUser.getName()+"--2");
        userTansService2.insertUser2(testUser);

        System.out.println("insertUser---,HelloTestController--");
        // int a = 1 / 0;
        return "新增成功";
    }


    @PutMapping("/insertThread")
    /* 线程相关：   */
    public Object insertUserThread( /* 参数注解 */  @RequestBody TestUser testUser) {
        //System.out.println(testUser.toString());
        userTansService.insertUser1(testUser);
        System.out.println(Thread.currentThread().getName());
        testUser.setName(testUser.getName()+"--2");


        System.out.println("insertUserThread---,HelloTestController--");
        // int a = 1 / 0;
        return "新增成功";

    }

    @PutMapping("/insertfuzi")
    /* 线程相关：   */
    public Object insertfuzi( /* 参数注解 */  @RequestBody TestUser testUser) {
        //System.out.println(testUser.toString());
       // inventoryAfterApproveRule.execute(new Object());
        Object o = new Object();

        System.out.println("insertUserThread---,HelloTestController--");
        // int a = 1 / 0;
        return "新增成功";

    }
















    @GetMapping("/query")
    /* 方法注解 */
    public Object queryAll( /* 参数注解 */  @RequestParam String name) {
        System.out.println(name);
        List<TestUser> list = helloListDao.queryAll();

        return list;

    }

    @GetMapping("/query2")
    /* 方法注解 */
    public Object queryAll( /* 参数注解 */   ) {
        System.out.println("name");
        List<TestUser> list = helloListDao.queryAll();

        return list;

    }


    @PutMapping("/update")
    /* 方法注解 */
    public Object updateUser( /* 参数注解 */   @RequestBody TestUser testUser) {
        System.out.println(testUser.toString());
        helloListDao.updateUser(testUser);

        return "修改成功";

    }

    @DeleteMapping("/delete")
    /* 方法注解 */
    public Object deleteUser( /* 参数注解 */  @RequestParam int id) {
        System.out.println(id);
        try {
            helloListDao.deleteUser(id);
        } catch (Exception e) {
            System.out.println(e);
        }

        return "删除成功";

    }
}
